<template>
	<view>
		<view class="account-top">
			<image class="headPhoto" mode="aspectFit" src="../../static/headPhoto.png"></image>
			<view class="account-name">{{this.username}}</view>
		</view>
		<view class="task">
			<view class="task-tip">
				<view style="width: 75%;">今日巡检任务如下：</view>
				<view style="align-self: flex-end;" @tap="taskHistory">历史任务></view>
			</view>
			
			<view class="task-body">
				<view v-for="(task, index) in taskList" :key="index" class="task-content">
					<view class="task-left">
						<view class="task-name">{{task.inspectionWork.inspectionWorkName}}</view>
						<view class="task-area">区域：{{task.map.mapName}}</view>
						<view class="task-status">
							<view>状态：</view>
							<view>
								<view style="color: #0A60BD;" v-if="task.inspectionWork.inspectionWorkStatus === 0">处理中</view>
								<view style="color: red;"  v-else-if="task.inspectionWork.inspectionWorkStatus === 1">已超时</view>
								<view style="color: #4CAF50;" v-else-if="task.inspectionWork.inspectionWorkStatus === 2">已完成</view>
								<view style="color: grey;"  v-else-if="task.inspectionWork.inspectionWorkStatus === 3">已取消</view>
								<view style="color: grey;" v-else-if="task.inspectionWork.inspectionWorkStatus === 4">未开始</view>
								<view v-else>未知状态</view>
							</view>
						
						</view>
						<view class="task-botom">
							<view class="task-schedule">进度：{{task.inspectionWork.inspectionWorkFinish}}/{{task.inspectionWork.inspectionWorkNumber}}</view>
							<view class="task-time">
								<view class="time-start">开始时间：{{formatDate(task.inspectionWork.inspectionStartTime)}}</view>
								<view class="time-end">限定完成时间：{{task.inspectionWork.inspectionLimitTime}}小时</view>
							</view>
						</view>
					</view>
					<view class="task-right" @tap="goToTaskInfo(task)">详情-></view>
				</view>
				<view class="tips">没有更多数据了</view>
			</view>
		</view>
	</view>
</template>

<script>
import request from '../../common/request.js'
export default {
	data() {
		return {
			username: '',
			completed: 0,
			totalTasks: 100,
			userId : 0,
			taskList: [],
			startTime:"",
			endTime:"",
		}
	},
	computed: {
		startDate() {
			return this.getDate('start');
		},
		endDate() {
			return this.getDate('end');
		}
	},
	onShow(){
		let userInfo = uni.getStorageSync('userInfo')
		this.username = userInfo.realName;
		this.userId = userInfo.userId;
		this.startTime = this.getDate()
		this.endTime = this.getDate()
		this.getInspection()
	},
	methods: {
		formatDate(dateString) {
			dateString = dateString.replace(" ", "+")
			const timestamp = Date.parse(dateString);
			
			if (isNaN(timestamp)) {
			  console.log("无效时间");
			  return "无效时间"
			} else {
			  const date = new Date(timestamp);
			  date.setMilliseconds(0);  // 精确到秒
			
			  // 获取年、月、日、时、分、秒
			  const offset = 8;
			  const year = date.getUTCFullYear();
			  const month = String(date.getUTCMonth() + 1).padStart(2, '0');  // 月份从0开始，所以加1
			  const day = String(date.getUTCDate()).padStart(2, '0');
			  const hours = String(date.getUTCHours()+offset).padStart(2, '0');
			  const minutes = String(date.getUTCMinutes()).padStart(2, '0');
			  const seconds = String(date.getUTCSeconds()).padStart(2, '0');
			
			  // 格式化为 `YYYY:MM:DD:HH:mm:ss`
			  const formattedDate = `${year}:${month}:${day} ${hours}:${minutes}:${seconds}`;
			  console.log(formattedDate);  // 输出：2024:10:12:14:00:01
			  return formattedDate
			}
		},
		getDate(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();
			
			if (type === 'start') {
				year = year - 5;
			} else if (type === 'end') {
				year = year;
			}
			month = month > 9 ? month : '0' + month;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		},
		goToTaskInfo(task) {
			let schedule = task.inspectionWork.inspectionWorkFinish+'/'+task.inspectionWork.inspectionWorkNumber
			uni.navigateTo({
				url: `../tasks/taskInfo?name=${task.inspectionWork.inspectionWorkName}&status=${task.inspectionWork.inspectionWorkStatus}&schedule=${schedule}&startTime=${task.inspectionWork.inspectionStartTime}&limitTime=${task.inspectionWork.inspectionLimitTime}&finishTime=${task.inspectionWork.inspectionEndTime}&mapId=${task.map.mapId}&mapName=${task.map.mapName}&inspectionWorkId=${task.inspectionWork.inspectionWorkId}`
			})
		},
		getInspection(){
			console.log(this.startTime+" "+this.endTime)
			request(`/inspection/search/${this.userId}`, 'POST', {
				"startTime": this.startTime,
				"endTime": this.endTime,
				"searchUserId": this.userId,
				"mapId": -1
			}).then(res => {
				
				console.log("查询任务数据完成：")
				console.log(res)
				this.taskList = res.data.data
				console.log(this.taskList)
			})
		},
		//查看历史任务
		taskHistory(){
			uni.navigateTo({
				url:"/pages/tasks/taskHistory/taskHistory"
			})
		}
	}
}
</script>

<style scoped>
.account-top {
	background-color: white;
	height: 80px;
	display: flex;
	align-items: center;
	padding-left: 20px;
}
.headPhoto {
	width: 60px;
	height: 60px;
	margin-right: 20px;
}
.account-name {
	width: 50%;
	font-size: 30px;
}
.task-tip {
	padding: 20px;
	display: flex;
}
.task-body {
}
.task-content {
	padding-bottom: 20px;
	display: flex;
	padding: 20px;
	background-color: #fff;
	margin: 10px;
	border-radius: 20px;
	box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.15);
}
.task-left {
	display: flex;
	flex-direction: column;
	width: 80%;
}
.task-name {
	font-size: 25px;
}
.task-botom {
	display: flex;
}
.task-schedule {
	text-align: left;
	align-self: center;
	width: 50%;
}
.task-time {
	font-size: 10px;
}
.task-right {
	align-self: center;
}
.tips {
	text-align: center;
}
.task-status{
	margin-bottom: 10px;
	display: flex;
}
</style>
